package com.swt.dao.impl.W_impl;

import com.swt.dao.W_dao.ClubDao;
import com.swt.domain.Club;
import com.swt.util.GetCurrentTime;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class ClubDaoImpl implements ClubDao {
    @Resource
    private HibernateTemplate hibernateTemplate;
    @Override
    public Club checkLogin(Club club) {
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createQuery("from Club where clubPhone=?");
        Club club1=(Club)query.setParameter(0,club.getClubPhone()).uniqueResult();
        return club1;
    }

    @Override
    public void register(Club club) {
        club.setClubStatus("待审核");
        club.setClubRegmonth(GetCurrentTime.getMonth());
        club.setClubRegdate(GetCurrentTime.getToday());
        hibernateTemplate.save(club);
    }


    /**
     * 获取所有馆主信息,分页查询5条
     * */
    @Override
    public List<Club> listToAll(int page,Club club) {
        if(club.getClubName()==""){
            Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
            Query query=session.createQuery("from Club");
            query.setFirstResult(page);
            query.setMaxResults(5);

            return query.list();
        }else
            return hibernateTemplate.findByExample(club,page,5);

    }

    @Override
    public void delete(Club club) {
        hibernateTemplate.delete(club);
    }

    @Override
    public void stop(Club club) {
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createQuery("update Club c set c.clubStatus=? where c.clubPhone=?");
        query.setParameter(0,club.getClubStatus());
        query.setParameter(1,club.getClubPhone());
        int result=query.executeUpdate();
        System.out.println(result);
    }

}
